﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StockService.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the StockService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Services
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using DocProcessingRepository.Interfaces;
  using Entities;
  using Exceptions;
  using ServiceInterfaces;

  public class StockService : IStockService
  {
    private readonly IStockRepository stockRepository;

    public StockService(IStockRepository stockRepository)
    {
      this.stockRepository = stockRepository;
    }

    public List<StockEntity> GetStockByType(String stockType, String filter)
    {
      try
      {
        String sqlFilter = String.Format("%{0}%", filter);
        return this.stockRepository.GetStockByType(stockType, sqlFilter);
      }
      catch (Exception e)
      {
        throw new DocProcessingException("Unable to get stock for type", e);
      }
    }

    public StockEntity CreateStock(
      Int32 stockTypeId,
      String code,
      String description,
      Int32 weight,
      Int32 sizeX,
      Int32 sizeY,
      Char facing,
      String colour)
    {
      try
      {
        String stockType = this.GetStockTypeName(stockTypeId);

        // Effectively filters out any stock where the StockType or description differ
        // Description still needs to be checked in case the description is an empty string
        List<StockEntity> existingStock = this.stockRepository.GetStockByType(stockType, description);

        Boolean stockAlreadyExists =
          existingStock.Any(
            s =>
            s.Code == code && s.Description == description && s.Weight == weight && s.SizeX == sizeX && s.SizeY == sizeY
            && Convert.ToChar(s.Facing) == facing && s.Colour == colour);

        if (stockAlreadyExists)
        {
          throw new DocProcessingException("Duplicate stock item found");
        }

        return this.stockRepository.CreateStock(stockTypeId, code, description, weight, sizeX, sizeY, facing, colour);
      }
      catch (DocProcessingException)
      {
        throw;
      }
      catch (Exception e)
      {
        throw new DocProcessingException("Unable to create stock", e);
      }
    }

    public Int32 GetStockTypeId(String stockType)
    {
      // StockTypeId hardcoded against their names, as we do not currently have a StockType repository
      switch (stockType)
      {
        case "LaserStock":
          return 1;
        case "Insert":
          return 2;
        case "Outer":
          return 3;
        default:
          throw new Exception("StockType not supported");
      }
    }

    private String GetStockTypeName(Int32 stockTypeId)
    {
      // StockTypeId hardcoded against their names, as we do not currently have a StockType repository
      switch (stockTypeId)
      {
        case 1:
          return "LaserStock";
        case 2:
          return "Insert";
        case 3:
          return "Outer";
        default:
          throw new Exception("StockTypeId not supported");
      }
    }
  }
}
